package org.prototype.bank.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.prototype.bank.dao.AccountDao;
import org.prototype.bank.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class JdbcAccountDao implements AccountDao {

	protected final Logger logger = Logger.getLogger(getClass());

	private SimpleJdbcTemplate simpleJdbcTemplate;
	private SimpleJdbcInsert insertAccount;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
		this.insertAccount = new SimpleJdbcInsert(dataSource).withTableName(
				"account").usingGeneratedKeyColumns("ID");
	}
	
	// public List<Account> list(String city) {
	// String sql = "SELECT * FROM Account WHERE CITY = ?";
	//
	// RowMapper<PostCode> mapper = new RowMapper<PostCode>() {
	// public PostCode mapRow(ResultSet rs, int rowNum)
	// throws SQLException {
	// PostCode postcode = new PostCode();
	//
	// postcode.setPostalCode(rs.getString("POST_CODE"));
	// postcode.setLongitude(rs.getDouble("LONGITUDE"));
	// postcode.setLatitude(rs.getDouble("LATITUDE"));
	// postcode.setCity(rs.getString("CITY"));
	// postcode.setProvinceCode(rs.getString("PROVINCE_CODE"));
	//
	// return postcode;
	// }
	// };
	// List<PostCode> results = simpleJdbcTemplate.query(sql, mapper,
	// city);
	// logger.info("Found " + results.size() + " vendors");
	// return results;
	// }

	public Account findAccount(String accountId) {
		String sql = "SELECT * FROM account WHERE ACCOUNT_ID = ?";

		RowMapper<Account> mapper = new RowMapper<Account>() {
			public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
				Account account = new Account();
				account.setId(rs.getLong("ID"));
				account.setBalance(rs.getDouble("BALANCE"));
				account.setAccountId(rs.getString("ACCOUNT_ID"));
				return account;
			}
		};
		return this.simpleJdbcTemplate.queryForObject(sql, mapper, accountId);
	}

	public Account createAccount(Account account) {
		
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(
				account);
		Number newId = insertAccount.executeAndReturnKey(parameters);
		account.setId(newId.longValue());
		return account;
	}

	public List<Account> listAccounts() {
		String sql = "SELECT * FROM account";
		RowMapper<Account> mapper = new RowMapper<Account>() {
			public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
				Account account = new Account();
				account.setId(rs.getLong("ID"));
				account.setBalance(rs.getDouble("BALANCE"));
				account.setAccountId(rs.getString("ACCOUNT_ID"));
				return account;
			}
		};
		return this.simpleJdbcTemplate.query(sql, mapper);
	}

}
